package com.example.springboot.service;

import com.example.springboot.entity.SysFile;
import com.example.springboot.entity.SysFileExample;
import com.example.springboot.mapper.SysFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: jyf2000jyf
 * @Date: 2022/4/30
 * @Time: 17:14
 * @Email: 904593011@qq.com
 * @Description:
 **/
@Service
public class FileServiceImpl implements FileService{

    @Autowired
    SysFileMapper fileMapper;

    @Override
    public SysFile selectMd5(String md5) {
        SysFileExample example = new SysFileExample();
        example.createCriteria().andMd5EqualTo(md5);
        List<SysFile> sysFiles = fileMapper.selectByExample(example);
        if (sysFiles.size() >= 1){
            return sysFiles.get(0);
        }
        return null;
    }

    @Override
    public List<SysFile> findAll(Integer pageNum, Integer pageSize, String name) {
        SysFileExample example = new SysFileExample();
        example.setOrderByClause("id desc limit " + pageNum + "," + pageSize);
        SysFileExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo(false);
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        return fileMapper.selectByExample(example);
    }

    @Override
    public Integer getAmount(String name) {
        SysFileExample example = new SysFileExample();
        SysFileExample.Criteria criteria = example.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        return (Integer) (int) fileMapper.countByExample(example);
    }

    @Override
    public boolean delete(Integer id) {
        SysFile sysFile = new SysFile();
        sysFile.setId(id);
        sysFile.setIsDelete(true);
        int i = fileMapper.updateByPrimaryKeySelective(sysFile);
        return i == 1 ? true : false;
    }
}
